/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui;

import java.io.InputStream;

import net.sourceforge.perforce.core.resources.RemoteResource;
import org.eclipse.compare.BufferedContent;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteResource;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PerforceResourceEditionNode extends BufferedContent implements IStructureComparator, ITypedElement
{
	private RemoteResource resource;
	private Object[] children;
	
	public PerforceResourceEditionNode(RemoteResource resource)
	{
		this.resource = resource;
	}

	/**
	  * Returns the associated RemoteResource.
	  */
	public RemoteResource getRemoteResource()
	{
		return resource;
	}
	
	/*
	 * @see BufferedContent#createStream()
	 */
	protected InputStream createStream() 
		throws CoreException
	{
		if (resource == null) 
			return null;

		try 
		{
			return resource.getContents(new NullProgressMonitor());
		} 
		catch (TeamException e) 
		{
			throw new CoreException(e.getStatus());
		}
	}

	/*
	 * @see IStructureComparator#getChildren()
	 */
	public Object[] getChildren()
	{
		if (children == null)
		{
			if (resource == null)
			{
				children = new PerforceResourceEditionNode[0];
			}
			else
			{
				try 
				{
					IRemoteResource[] members = resource.members(new NullProgressMonitor());
					children = new PerforceResourceEditionNode[members.length];
					for (int i = 0; i < members.length; i++) {
						children[i] = new PerforceResourceEditionNode((RemoteResource)members[i]);
					}
				} 
				catch (TeamException e) 
				{
					PerforceUIPlugin.log(e.getStatus());
				}
			}			
		}		
		
		return children;
	}

	/*
	 * @see ITypedElement#getName()
	 */
	public String getName()
	{
		return resource == null ? "" : resource.getName(); //$NON-NLS-1$
	}

	/*
	 * @see ITypedElement#getImage()
	 */
	public Image getImage()
	{
		return CompareUI.getImage(resource);
	}

	/*
	 * @see ITypedElement#getType()
	 */
	public String getType()
	{
		if (resource == null) 
			return UNKNOWN_TYPE;

		if (resource.isContainer()) 
			return FOLDER_TYPE;
		
		String name = resource.getName();
		name = name.substring(name.lastIndexOf('.') + 1);
		return name.length() == 0 ? UNKNOWN_TYPE : name;
	}

	/**
	 * Returns true if both resources names are identical.
	 * The content is not considered.
	 * @see IComparator#equals
	 */
	public boolean equals(Object other) 
	{
		if (other instanceof ITypedElement) 
		{
			String otherName = ((ITypedElement)other).getName();
			return getName().equals(otherName);
		}
		return super.equals(other);
	}

	/**
	 * @see IComparator#equals
	 */
	public int hashCode() 
	{
		return getName().hashCode();
	}
}
